<template>
  <div class="container">
    <el-form :inline="true" :model="search" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="search.cusName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="search.cusTel" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="活动区域">
        <el-select v-model="search.region" placeholder="活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="button">
      <el-button @click="onAdd" type="primary">新增客户</el-button>
    </div>
    <el-table
      border
      :data="list"
      style="width: 100%"
    class="table"
    >
      <el-table-column
        prop="cusName"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.cusId)"
            class="update"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.cusId)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex; justify-content: flex-end; margin-top: 20px">
      <el-pagination
        v-show="total>0"
        :total="total"
        background
        @current-change="getList"
        :current-page.sync="search.currentPage"
        :page-size.sync="search.size"
        layout="total, prev, pager, next"
      />
    </div>
  </div>
</template>

<script>
import { customerList, deleteCustomer } from '@/api/user'
  export default {
    name: 'Index',
    data() {
      return {
        search: {
          cusName: '',
          cusTel: '',
          currentPage: 1,
          size: 10
        },
        total: 0,
        list: [],
        tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }]
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        customerList(this.search).then(res => {
          console.log(res)
          this.list = res.data.records
          this.total = res.data.total
        })
      },
      onAdd() {
        this.$router.push({ name: 'AddCustomer' })
      },
      handleUpdate(id) {
        this.$router.push({ name: 'AddCustomer', query: { id } })
      },
      handleDelete(id) {
        deleteCustomer({ cusId: id }).then(res => {
          this.$message({
            message: '刪除成功',
            type: 'success'
          })
          this.getList();
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.container {
  padding: 50px;
  display: flex;
  flex-direction: column;
.button {
  display: flex;
  justify-content: flex-end;
}
  .table {
    margin-top: 20px;
  }
}

</style>
